<?php

class LetterEnvilope
{
    protected $_M_headers;
    protected $_M_letter;
    protected $_M_crlf;

    function __construct()
    {
        $this->_M_headers = array();
        $this->_M_letter    = '';
        $this->_M_crlf      = "\r\n";
    }

    function header($header, $value)
    {
        $this->_M_headers[$header] = $value;
    }

    function letter($content)
    {
        $this->_M_letter = $content;
    }

    function data()
    {
        $data = array();
        foreach ( $this->_M_headers as $header => $value ) {
            $data[] = $header . ': ' . $value;
        }

        $data[] = '';
        $body = explode($this->_M_crlf, $this->_M_letter);
        foreach ( $body as $line ) {
            $data[] = $line;
        }

        return $data;
    }

    function subject($value)
    {
        $this->header('Subject', $value);
    }

    function from($email, $name=null)
    {
        $value = '';
        if (! is_null($name)) {
            $value .= $name . ' ';
        }
        $this->header('From', $value. '<' . $email . '>');
    }

    function to($email, $name=null)
    {
        $value = '';
        if (! is_null($name)) {
            $value .= $name . ' ';
        }
        $this->header('To', $value. '<' . $email . '>');
    }

    function x_sender($email)
    {
        $this->header('X-Sender', '<'.$email.'>');
    }

    function return_path($email)
    {
        $this->header('Return-Path', '<'.$email.'>');
    }

    function errors_to($email)
    {
        $this->header('Errors-To', '<'.$email.'>');
    }

    function x_mailer($value)
    {
        $this->header('X-Mailer', $value);
    }

    function x_priority($value)
    {
        $this->header('X-Priority', $value);
    }

    function content_type($type, $encoding)
    {
        $this->header('Content-Type', $type . '; charset=' . $encoding);
    }

}

?>